<template>
  <div>
    <!-- 添加文章对话框 -->
    <el-form :model="form" ref="ruleForm" :rules="rules">
      <el-form-item label="文章标题:" :label-width="formLabelWidth"  prop="title">
        <el-input
          v-model="form.title"
          placeholder="请输入文章标签"
        ></el-input>
      </el-form-item>
      <!-- 富文本 -->
      <el-form-item label="文章内容:" :label-width="formLabelWidth" prop="articleBody">
        <quill-editor
          style="height: 200px"
          v-model="form.articleBody"
          ref="myQuillEditor"
          :options="editorOption"
        >
        </quill-editor>
      </el-form-item>

      <el-form-item
        style="margin-top: 75px"
        label="视频地址:"
        :label-width="formLabelWidth"
        prop="videoURL"
      >
        <el-input
          v-model="form.videoURL"
          placeholder="请输入视频地址"
        ></el-input>
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancelHandle()">取 消</el-button>
      <el-button type="primary" @click="addHandle()">确 定</el-button>
    </div>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/articles'
export default {
  name: 'articlesAdd',
  props: {
    dialogTableVisible: {
      type: Boolean,
      default: false
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    row: {
      type: Object,
      default: () => {}
    }

  },
  data () {
    return {
      formLabelWidth: '120px',
      // 富文本编辑器属性
      editorOption: {
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], // toggled buttons
            [{ list: 'ordered' }, { list: 'bullet' }, 'blockquote'], // 有序、无序列表、引用
            ['code-block', 'image', 'link'] // 代码块  链接、图片、视频
          ]
        }
      },
      form: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' }
        ],
        // 内容
        articleBody: [
          { required: true, message: '内容不能为空', trigger: 'blur' }
        ]

      }
    }
  },
  watch: {
    isEdit: {
      handler (nweVal) {
        if (nweVal) {
          this.form = { ...this.row }
        }
      },
      immediate: true // 初次进入立即执行监听
    }
  },
  created () {
  },
  mounted () {},
  methods: {
    // 富文本校验
    // contentRule () {
    //   this.$refs.addModelRef.validateField('content')
    // },

    // 添加
    async addHandle () {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let res = {}
          if (!this.isEdit) {
            res = await add({
              ...this.form
            })
            if (res.status !== 200) return
            this.$message.success('恭喜你添加成功')
            this.$refs.ruleForm.resetFields()
          } else {
            res = await update({
              ...this.form
            })
            if (res.status !== 200) return
            // console.log(res, 3452351)
            this.$message.success('恭喜你修改学科成功')
          }
          this.$emit('success')
        }
      })
    },
    // 取消
    cancelHandle () {
      this.$emit('success')
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>
